package com.jerry.template.module.mainpage.msg.activity;

import android.content.Intent;

import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;

import java.util.HashMap;

import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by chenhuan on 2016/5/12.
 */
public class LoginWithOtherAppActivity extends BaseActivity implements PlatformActionListener{

    private boolean isqq = true;

    @Override
    protected int getLayoutId(){
        return R.layout.activity_login;
    }

    
    @OnClick(R.id.msg_back)
    public void sumbit(){
        finish();
    }

    @OnClick(R.id.msg_qq)
    public void qqLogin(){
        QQ qqPlatform = new QQ(this);


        //是否已经鉴权
        if(qqPlatform.isAuthValid()){
            PlatformDb platformDb = qqPlatform.getDb();
            // 如果用户ID存在，则认为用户是合法用户，允许进入系统
           if(platformDb.isValid() && platformDb.getUserId() != null){
                Intent intent = new Intent();
                intent.setClass(this, LoginSuccessActivity.class);
                startActivity(intent);
                finish();
            }
            return;
        }

        qqPlatform.SSOSetting(false);
        qqPlatform.setPlatformActionListener(this);

        qqPlatform.showUser(null);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap){
        PlatformDb db = platform.getDb();
        if(platform.isValid() && platform.getDb().getUserId() != null){
            String userIcon = db.getUserIcon();
            String userName = db.getUserName();
            String userGender = db.getUserGender();

            Intent intent = new Intent(this, LoginSuccessActivity.class);
            intent.putExtra("userName", userName);
            intent.putExtra("userGender", userGender);
            intent.putExtra("userIcon", userIcon);
            intent.putExtra("isQq",isqq);

            startActivity(intent);
        }
    }

    @OnClick(R.id.msg_local)
    public void localLogin(){
        Intent intent = new Intent();
        intent.setClass(this, LoginSuccessActivity.class);
        startActivity(intent);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable){
        platform.removeAccount();
    }

    @Override
    public void onCancel(Platform platform, int i){
    }
}
